// pages/myorder_detail/myorder_detail.js
import QQMapWX from "../../util/qqmap-wx-jssdk.min.js";
let qqMap = new QQMapWX({
  key: 'Q5GBZ-LLN3P-FYADF-VB7UZ-7GESV-PWBCV' // 必填
});
var app = getApp();//引用app.js
var commonURL = app.data.baseURL;
var commonURLWW = app.data.testURL;
var appId = app.data.appid;
var appSecret = app.data.secret;
Page({
  data: {
    data: '',
    orderid: null,
    orderState: false,
    isGroup: false,
    isState: true,
    "navTab": ["订单状态", "订单详情"],
    "currentNavtab": "0",
    "zhuangtai": "",//根据状态判断显示的项目
    "detailTime": {
      buytime: "",
      paytime: "",
      kuaiditime: "",
      ordertime: ""
    },
    "orderDisplay": {
      FFtxta: "",
      FFtxtb: "",
      DDTJ: "none",//订单完成
      YJFH: "none",//已经发货
      ZFCG: "none",//支付成功
      SJFH: "none",//商家发货
      DDWC: "none",//订单完成
      DDTK: "none",//等待退款
      YJTK: "none",//已经退款
      BFTK: "none",//部分退款
      DDQX: "none",//订单取消
      TZHX: "none"//团长核销
    },
    "xiangqing": {},
    onclose_cover: false
  },
  onLoad: function (options) {
    console.log(options)
    var that = this;
    wx.getStorage({
      key: 'xiaoqu',
      success: function (res) {
        console.log(res)
        that.setData({
          comTel: res.data.item.commanderTel
        })
      },
    })
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          nickname:res.data.nickname
        })
      },
    })
    console.log(options.orderid);
    // wx.getUserInfo({})
    // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    var _scene = decodeURIComponent(options.scene);
    console.log('扫码信息');
    console.log(_scene);
    if (_scene !== 'undefined') {          //判断是不是扫码进来的
      this.post_order_check(_scene, function (data) {
        if (data.status == 0) {
          wx.showToast({
            title: data.result,
            icon: 'loading',
            duration: 2000
          })
          return;
        };
        that.render(_scene, app.data.openid, that);
      });
    } else {
      that.render(options.orderid, app.data.openid, that);
    }

    // 页面初始化 options为页面跳转所带来的参数  orderid
    // wx.request({
    //   url: commonURL+'op=order_detail', 
    //   data:{
    //     "id":options.orderid,
    //     "openid":app.data.openid
    //   },
    //   header: {
    //       'content-type': 'application/json'
    //   },
    //   success: function(res) {
    //     console.log(res.data);
    //     var data=res.data;
    //     var status=res.data.status;
    //     var DDTJ="none",ZFCG="none",DDFH="none",YJFH="none",DDWC="none",DDTK="none",YJTK="none",DDQX="none",FFtxta="",FFtxtb="",BFTK="none",kuaiditime="",ordertime="";
    //     switch (status){//根据订单状态显示不同内容
    //       case "0"://等待 付款******  
    //         DDTJ="block"; break;
    //       case "1"://支付成功  
    //         DDTJ="block";ZFCG="block"; break;
    //       case "8"://等待商家发货    0 1 8
    //         DDTJ="block";ZFCG="block";DDFH="block";FFtxta="等待发货";FFtxtb="请耐心等待商家发货"; break;
    //       case "2"://订单已发货  0 1 8 2
    //         DDTJ="block";ZFCG="block";DDFH="block";FFtxta="商家发货";FFtxtb="商家已经发货,请耐心等待";kuaiditime=""; break;  
    //       case "3"://订单完成  0 1 8 2 3
    //         DDTJ="block";ZFCG="block";DDFH="block";FFtxta="商家发货";FFtxtb="商家已经发货,请耐心等待";kuaiditime="";DDWC="block"; break;
    //       case "10"://等待退款 0 1 10
    //         DDTJ="block";ZFCG="block";DDFH="block";FFtxta="商家发货";FFtxtb="商家已经发货,请耐心等待";DDTK="block"; break;  
    //       case "4"://订单已退款  0 1 4
    //         DDTJ="block";ZFCG="block";DDFH="block";FFtxta="商家发货";FFtxtb="商家已经发货,请耐心等待";YJTK="block"; break;
    //       case "7"://订单已退款  0 1 10 7
    //         DDTJ="block";ZFCG="block";DDFH="block";FFtxta="商家发货";FFtxtb="商家已经发货,请耐心等待";YJTK="block"; break;
    //       case "6"://部分退款  0 1 8 2 6
    //         DDTJ="block";ZFCG="block";DDFH="block";FFtxta="商家发货";FFtxtb="商家已经发货,请耐心等待";kuaiditime="";BFTK="block"; break;
    //       case "9"://订单已取消  0 9****
    //         DDTJ="block";DDQX="blcok"; break;      
    //     }
    //     var goodsname = [];
    //     var gnum = [];
    //     var goodsprice = [];
    //     if(data.g_id == '0') {
    //       for(var i = 0; i < data.collect_list.length; i++) {
    //         goodsname.push(data.collect_list[i].goodsname);
    //         gnum.push(data.collect_list[i].num);
    //         goodsprice.push(data.collect_list[i].oprice);
    //       }
    //     }else{
    //       goodsname.push(data.goodsname)
    //       gnum.push(data.gnum);
    //       goodsprice.push(data.goodsprice);
    //     }
    //     that.setData({
    //       "orderDisplay":{
    //         FFtxta:FFtxta,
    //         FFtxtb:FFtxtb,
    //         DDTJ:DDTJ,
    //         ZFCG:ZFCG,
    //         DDFH:DDFH,
    //         YJFH:YJFH,
    //         DDWC:DDWC,
    //         YJTK:YJTK,
    //         DDTK:DDTK,
    //         DDQX:DDQX,
    //         BFTK:BFTK
    //       },
    //       "detailTime":{
    //         buytime:data.createtime,
    //         paytime:data.ptime,
    //         kuaiditime:kuaiditime,
    //         ordertime:ordertime
    //       },
    //       "xiangqing":{
    //         comp:app.data.comName,
    //         title:goodsname,
    //         price:goodsprice,
    //         num:gnum,
    //         yunfei:data.freight,
    //         sumPrice:data.pay_price,
    //         name:data.addname,
    //         address:data.address,
    //         people:data.salename,
    //         hxTime:data.hexiaotime,
    //         orderNum:data.orderno,
    //         orderTime:data.createtime,
    //         orderPay:"微信支付",
    //         dispatchtype: data.dispatchtype
    //       }
    //     })
    //   }
    // })

    // var img = wx.getStorageSync('code_img')
    // if(img){
    //   that.setData({
    //     code_img: img
    //   })
    //   return;
    // }
    if (options.orderno) {
      this.setData({
        orderid: options.orderno
      })
    }
    wx.request({                   //获取二维码
      url: commonURLWW + 'op=qrcode',
      data: {
        openid: app.data.openid,
        orderno: options.orderid
        // options.orderid
        // appid: appId,
        // secret: app.data.secret,
        // path: "pages/myorder_detail/myorder_detail",
        // scene: options.orderid
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          code_img: res.data.url
        })
        wx.setStorage({
          key: "code_img",
          data: res.data
        })
      }
    })
  },
  switchTab: function (e) {
    this.setData({
      currentNavtab: e.currentTarget.dataset.index,
      isState: !this.data.isState
    });
  },
  //联系团长
  goPhone: function () {
    if (this.data.datas.commander_mobile) {
      wx.makePhoneCall({
        phoneNumber: this.data.datas.commander_mobile,
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          wx.showToast({
            title: '拨打电话失败',
          })
        }
      })
    } else {
      wx.showToast({
        title: '获取电话失败',
      })
    }

  },
  //打开地图导航
  goMap: function () {
    if (this.data.data.lat && this.data.data.lng) {
      wx.openLocation({
        latitude: parseInt(this.data.data.lat),
        longitude: parseInt(this.data.data.lng),
        success: function (res) {
          console.log(res)
        },
        fail: function (res) {
          wx.showToast({
            title: '获取自提点经纬度失败',
            icon: 'none'
          })
        }
      })
    } else {
      wx.showToast({
        title: '获取自提点失败',
        icon: 'none'
      })
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  close_cover: function () {
    this.setData({
      onclose_cover: false
    })
  },
  onshow_cover: function () {
    this.setData({
      onclose_cover: true
    })
  },
  post_order_check: function (orderno, callBack) {
    wx.request({
      url: commonURLWW + 'op=order_check',
      data: {
        orderno: orderno,
        openid: app.data.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        callBack(res.data);
      }
    })
  },
  render: function (orderno, openid, that) {
    wx.request({
      url: commonURL + 'op=order_detail',
      data: {
        "id": orderno,
        "openid": openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          datas: res.data
        })
        console.log(res.data, 8868);
        var data = res.data;
        var status = res.data.status;
        var DDTJ = "none", ZFCG = "none", DDFH = "none", YJFH = "none", DDWC = "none", DDTK = "none", YJTK = "none", DDQX = "none", FFtxta = "", FFtxtb = "", BFTK = "none", kuaiditime = "", ordertime = "", TZHX = 'none';

        switch (status) {//根据订单状态显示不同内容
          case "0"://等待 付款******  
            DDTJ = "block"; break;
          case "1"://支付成功  
            DDTJ = "block"; ZFCG = "block"; break;
          case "8"://等待商家发货    0 1 8
            DDTJ = "block"; ZFCG = "block"; DDFH = "block"; FFtxta = "等待发货"; FFtxtb = "请耐心等待商家发货"; break;
          case "2"://订单已发货  0 1 8 2
            DDTJ = "block"; ZFCG = "block"; DDFH = "block"; FFtxta = "商家发货"; FFtxtb = "商家已经发货,请耐心等待"; kuaiditime = ""; break;
          case "3"://订单完成  0 1 8 2 3
            DDTJ = "block"; ZFCG = "block"; DDFH = "block"; FFtxta = "商家发货"; FFtxtb = "商家已经发货,请耐心等待"; kuaiditime = ""; DDWC = "block"; break;
          case "10"://等待退款 0 1 10
            DDTJ = "block"; ZFCG = "block"; DDFH = "block"; FFtxta = "商家发货"; FFtxtb = "商家已经发货,请耐心等待"; DDTK = "block"; break;
          case "4"://订单已退款  0 1 4
            DDTJ = "block"; ZFCG = "block"; DDFH = "block"; FFtxta = "商家发货"; FFtxtb = "商家已经发货,请耐心等待"; YJTK = "block"; break;
          case "7"://订单已退款  0 1 10 7
            DDTJ = "block"; ZFCG = "block"; DDFH = "block"; FFtxta = "商家发货"; FFtxtb = "商家已经发货,请耐心等待"; YJTK = "block"; break;
          case "6"://部分退款  0 1 8 2 6
            DDTJ = "block"; ZFCG = "block"; DDFH = "block"; FFtxta = "商家发货"; FFtxtb = "商家已经发货,请耐心等待"; kuaiditime = ""; BFTK = "block"; break;
          case "9"://订单已取消  0 9****
            DDTJ = "block"; DDQX = "blcok"; break;
          case "5"://订单核销  0 9****
            DDTJ = "block"; ZFCG = "block"; TZHX = "block"; FFtxta = "团长核销"; FFtxtb = "团长已核销,请耐心等待"; kuaiditime = ""; break;
        }
        var goodsname = [];
        var gnum = [];
        var goodsprice = [];
        var salename='';
        that.setData({
          list5: data.collect_list
        })
        if (data.g_id == '0') {
          for (var i = 0; i < data.collect_list.length; i++) {
            goodsname.push(data.collect_list[i].goodsname);
            gnum.push(data.collect_list[i].num);
            goodsprice.push(data.collect_list[i].num);
          }
        } else {
          goodsname.push(data.goodsname)
          gnum.push(data.gnum);
          goodsprice.push(data.goodsprice);
        }
        if (data.is_tuan == 1) {
          that.setData({
            isGroup: true
          })
        }
        if (data.salename == '' || data.salename === null){
          salename = that.data.nickname
        }else{
          salename = data.salename
        }
        that.setData({
          "orderDisplay": {
            FFtxta: FFtxta,
            FFtxtb: FFtxtb,
            DDTJ: DDTJ,
            ZFCG: ZFCG,
            DDFH: DDFH,
            YJFH: YJFH,
            DDWC: DDWC,
            YJTK: YJTK,
            DDTK: DDTK,
            DDQX: DDQX,
            BFTK: BFTK,
            TZHX: TZHX
          },
          "detailTime": {
            buytime: data.createtime,
            paytime: data.ptime,
            kuaiditime: kuaiditime,
            ordertime: ordertime
          },
          
          "xiangqing": {
            comp: app.data.comName,
            title: goodsname,
            price: goodsprice,
            num: gnum,
            yunfei: data.freight,
            sumPrice: data.pay_price,
            name: data.addname,
            address: data.checkstore,
            people: salename,
            hxTime: data.hexiaotime,
            orderNum: data.orderno,
            orderTime: data.createtime,
            orderPay: "微信支付",
            dispatchtype: data.dispatchtype
          },
          data: res.data
        })
        var nums = [];
        var tem = {};
        for (var i = 0; i < gnum.length; i++) {
          tem = {
            num: gnum[i],
            name: goodsname[i],
            price: goodsprice[i],
            status: that.data.datas.collect_list[i].status
          }
          nums.push(tem)
          console.log(nums)
        }
        console.log(nums)
        that.setData({
          nums: nums
        })
      }
    })
  },
  receipt: function (e) {
    console.log(e)
    var that = this
    var id = e.target.dataset.id
    var obj = {}
    obj.orderno = id
    obj.uniacid = app.data.GZHid
    wx.showModal({
      title: '提示',
      content: '确认收货吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://min.lexiangpingou.cn/addons/lexiangpingou/lxapi/xcx_pay.php?op=receipt',
            data: obj,
            success: function (res) {
              console.log(res)
              that.render(id, app.data.openid, that)
            }
          })
        }
      }
    })

  },
  toHome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  toGroupInfo: function (e) {
    wx.navigateTo({
      url: '/pages/mygroup_detail/mygroup_detail?tuan_id=' + this.data.data.tuan_id + '&g_id=' + this.data.data.g_id,
    })
  }
})